package com.ftg.learn.chapter05;

import com.ftg.learn.chapter04.MyList;

import java.util.Objects;

/**
 * 线性表的 链式存储 结构
 * @author kangjx
 * @createTime 2022/6/30
 */
public class ItemList<T> implements MyList<T> {

    private Item<T> head;

    /**
     * 尾巴
     */
    private Item<T> tail;

    private int count = 0;

    public ItemList(){
        head = null;
        tail = null;
    }

    public ItemList(T val){
        initHead(val);
    }

    public boolean initHead(T val){
        if(Objects.isNull(head)){
            head = new Item(val);
            tail = head;
            return true;
        }
        return false;
    }

    @Override
    public boolean add(T val){
        if(!initHead(val)){
            tail.setNext(new Item(val));
            tail = tail.getNext();
        }
        count++;
        return true;
    }


    @Override
    public T get(int index) {
       return this.getItem(index).getVal();
    }


    public Item<T> getItem(int index) {
        lessThenZero(index);
        grentThenMax(index,this.size());
        switch (index){
            case 0 :
                return this.getHead();
            default:
                return this.findItem(index);
        }
    }

    public Item<T> findItem(int index){
        if(index == 0){
            return this.getHead();
        }
        int sum = 1;
        Item<T> temp = head;
        while(temp.getNext() != null){
            if(sum++ == index){
                return temp.getNext();
            }
            temp = temp.getNext();
        }
        return null;
    }

    /**
     * 返回头节的值
     * @return
     */
    public T getHeadVal(){
        return head.getVal();
    }

    /**
     * 返回头节的值
     * @return
     */
    public T getTailVal(){
        return tail.getVal();
    }


    /**
     * 返回头节
     * @return
     */
    public Item<T> getHead(){
        return head;
    }

    /**
     * 返回尾
     * @return
     */
    public Item<T> getTail(){
        return tail;
    }

    @Override
    public boolean update(int index, T newVal) {
        Item<T> currItem = this.getItem(index);
        currItem.setVal(newVal);
        return true;
    }

//    //这种方式明白了 ?
//    @Override
//    public boolean delete(int index) {
//        this.getItem(index-1).setNext(this.getItem(index+1));
//        return true;
//    }

    @Override
    public boolean delete(int index) {
        lessThenZero(index);
        grentThenMax(index,size());
        if(0 == index){
            deleteHead();
            return true;
        }
        if(index == size() - 1){
            deleteTail();
            return true;
        }
        deleteOther(index);
        return true;
    }

    public boolean deleteHead(){
        this.head = this.head.getNext();
        count--;
        return true;
    }

    public boolean deleteOther(int index){
        Item<T> temp = this.findItem(index - 1);
        temp.setNext(temp.getNext().getNext());
        count--;
        return true;
    }

    public boolean deleteTail(){
        if(this.size() -2 >= 0){
            tail = this.getItem(this.size()-2);
            tail.setNext(null);
        }else{
            head = null;
            tail = null;
        }
        count--;
        return true;
    }

    @Override
    public boolean delete(T val) {
        return false;
    }

    @Override
    public int size() {
        return this.count;
    }

    public static void main(String[] args) {
        MyList<Integer> m1 = new ItemList<>();
        m1.add(2);
        m1.add(3);
        m1.add(4);

        for(int i = 0;i < m1.size();i++){
            System.out.print(m1.get(i)+"\t");
        }
        m1.delete(1);
        System.out.println();
        for(int i = 0;i < m1.size();i++){
            System.out.print(m1.get(i)+"\t");
        }
//        m1.update(1,123);
//        for(int i = 0;i < m1.size();i++){
//            System.out.println(m1.get(i));
//        }
//        int a = 1;
//        System.out.println(a++);
//        System.out.println(a);
    }
}
